package com.shuoyue.ycgk.ui.shop.address;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.address.OrderAddressBean;
import com.shuoyue.ycgk.views.dialog.common.HintConfirmDialog;


import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SelectAddressActivity extends BaseMvpActivity<SelectAddressContract.Presenter> implements SelectAddressContract.View {


    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    AddressSelectAdapter addressSelectAdapter;
    int type = 0;//0管理/1选择
    int selectId;

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getAddress();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_address;
    }

    @Override
    protected void initView() {
        pageTitle.setText("收货地址");
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        addressSelectAdapter = new AddressSelectAdapter(null);
        recyclerView.setAdapter(addressSelectAdapter);
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getAddress();
            refreshLayout.finishRefresh();
        });
        addressSelectAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.lay_all:
                    if (type == 1) {
                        setResult(RESULT_OK, new Intent().putExtra("add", addressSelectAdapter.getItem(position)));
                        finish();
                    }
                    break;
                case R.id.edit:
                    startActivity(new Intent(mContext, EditAddressActivity.class).putExtra("add", addressSelectAdapter.getItem(position)));
                    break;
                case R.id.delete:
                    HintConfirmDialog hintConfirmDialog = new HintConfirmDialog(mContext, "删除", "是否删除地址");
                    hintConfirmDialog.setOkListener(view1 -> {
                        mPresenter.delete(addressSelectAdapter.getItem(position).getId());
                        hintConfirmDialog.dismiss();
                    });
                    hintConfirmDialog.show();
                    break;
            }
        });
        registEmptyView(addressSelectAdapter);
        registFinishLoad(refreshlayout);
    }

    @Override
    public void initData() {
        type = getIntent().getIntExtra("type", 0);
        selectId = getIntent().getIntExtra("id", -1);
        mPresenter = new SelectAddressContract.Presenter();
        mPresenter.attachView(this);
    }

    @OnClick({R.id.back, R.id.add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                setResult(RESULT_CANCELED);
                finish();
                break;
            case R.id.add:
                startActivity(new Intent(mContext, EditAddressActivity.class));
                break;
        }
    }


    @Override
    public void setAddress(List<OrderAddressBean> data) {
        for (OrderAddressBean orderAddressBean : data) {
            if (selectId == orderAddressBean.getId()) {
                orderAddressBean.setSelect(true);
            } else {
                orderAddressBean.setSelect(false);
            }
        }
        addressSelectAdapter.resetData(data);
    }

    @Override
    public void editSuc() {

    }

    @Override
    public void addSuc() {

    }

    @Override
    public void deleteSuc() {
        mPresenter.getAddress();
    }
}
